package ink.ykb.demo.user.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import ink.ykb.demo.config.CodeConstant;
import ink.ykb.demo.config.MessageUtil;
import ink.ykb.demo.config.ResponseMsg;
import ink.ykb.demo.permission.annotation.Function;
import ink.ykb.demo.user.entity.UserInfo;
import ink.ykb.demo.user.service.UserInfoService;
import ink.ykb.demo.user.service.dto.UserInfoPage;
import ink.ykb.demo.util.PageInfo;

/**
 * 用户相关服务
 * 
 * @author mading
 *
 */
@RestController
@RequestMapping("/userInfo")
public class UserInfoController {

  @Autowired
  private UserInfoService userInfoService;
  @Autowired
  private MessageUtil messageUtil;

  /**
   * 添加用户
   * 
   * @param userInfo
   */
  @PostMapping(value = "/add")
  @Function("admin.userInfo.add")
  public ResponseMsg<String> add(UserInfo userInfo) {
    userInfoService.add(userInfo);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 更新用户
   * 
   * @param userInfo
   */
  @Function("admin.userInfo.update")
  @PostMapping(value = "/update")
  public ResponseMsg<String> update(UserInfo userInfo) {
    userInfoService.update(userInfo);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 删除用户
   * 
   * @param userInfoId
   */
  @Function("admin.userInfo.delete")
  @PostMapping(value = "/delete")
  public ResponseMsg<String> delete(@RequestParam("userId") Long userId) {
    userInfoService.delete(userId);
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS));
  }

  /**
   * 分页查询用户列表
   * @param userInfoPage
   * @return
   */
  @Function("admin.userInfo.page")
  @GetMapping(value = "/page")
  public ResponseMsg<PageInfo<UserInfo>> getUserInfoPage(
		  UserInfoPage userInfoPage) {
    return new ResponseMsg<>(CodeConstant.SUCCESS, messageUtil.getMsg(CodeConstant.SUCCESS),
        userInfoService.getUserInfoPage(userInfoPage));
  }
  
  /**
   * 获取用户信息
   * 
   * @param userInfoId
   * @return
   */
  @Function("admin.userInfo.get")
  @GetMapping(value = "/get")
  public ResponseMsg<UserInfo> getUserInfo(@RequestParam("userId") Long userId) {
    return new ResponseMsg<>(CodeConstant.SUCCESS,messageUtil.getMsg(CodeConstant.SUCCESS),
        userInfoService.getUserInfo(userId));
  }
}
